<?
if (file_exists(RGL_FS_WEB_ROOT.'/stop.txt')) {
	header("location: ".RGL_URL_ROOT."/app/_com/stop.php");
	exit();
}
require_once(RGL_FS_WEB_ROOT."/app/_inc/inc.set1.php");
require_once(RGL_FS_WEB_ROOT."/app/_inc/class.rglSession.php");
if (!isset($isPopUp)) $isPopUp = false;
if (!isset($isReloadable)) $isReloadable = false;

if(phpversion() < 5){
	die(_t("PHP 5 or greater is needed."));
}

ini_set("display_error", 1);
error_reporting(E_ALL);
// configura il gestore dell'errore definito dall'utente
$oldErrorHandler = set_error_handler("rgl_error_handler");

//Disattiva il caching della pagina
if (headers_sent()) {
	rglLogWarn("inviati headers prima di inc.session");
	//trigger_error(_t("inviati headers prima di inc.session"),E_USER_ERROR);
}

//header("Pragma: public"); 
//header("Expires: 0"); 
//header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
//header("Cache-Control: private",false); 

if (!isset($_REQUEST['xsn'])) {
	rglErrSess(_t("sessione non identificata (1)"));
	exit();
}

session_start();

$ax = explode('.',$_REQUEST['xsn']);
$subSess = $ax[0];
if (!isset($ax[1])) {
	if (!$isPopUp) {
		rglErrSess(_t("codice sessione anomalo (%s)",$_REQUEST['xsn']));
		exit();
	}
	$pageSeq = 0;
} else {
	$pageSeq = intval($ax[1]);
}

$userObjKey = RGL_SESS_OBJ_PREF.$subSess;
if(!isset($_SESSION[$userObjKey])){
	rglErrSess(_t("sessione scaduta (%s)",$_REQUEST['xsn']));
	exit();
}
if(!is_object($_SESSION[$userObjKey])){
	rglErrSess(_t("sessione non identificata (%s)",$_REQUEST['xsn']));
	exit();
}
$objSess = $_SESSION[$userObjKey];
if($objSess->login == '@@logout@@' and !isset($isLogOut)){
	rglErrSess(_t("eseguito log-out !"));
	exit();
}

//var_dump($objSess);die('x');
define('SESSOBJ',RGL_SESS_OBJ_PREF.$subSess);
define('SUB_SESS',$subSess);
define('ID_UTENTE', $objSess->idUtente);
define('LOGIN', $objSess->login);

$aDebugParam = unserialize(RGL_DEBUG_PARAM);
$flagDisplay='N';
$bDisplayGet=$bDisplayPost=$bDisplayUser=$bDisplaySess=false;
if (is_array($aDebugParam)) {
	if (isset($aDebugParam['users']['*']))
		$flagDisplay = $aDebugParam['users']['*'];
	if (is_object($objSess) and isset($aDebugParam['users'][$objSess->login]))
		$flagDisplay = $aDebugParam['users'][$objSess->login];
	if (is_object($objSess) and $objSess->backDoor) $flagDisplay = 'D';
	if ($flagDisplay=='D') {
		$bDisplayGet=(isset($aDebugParam['get']) and $aDebugParam['get'] === true)?true:false;
		$bDisplayPost=(isset($aDebugParam['post']) and $aDebugParam['post'] === true)?true:false;
		$bDisplayUser=(isset($aDebugParam['user']) and $aDebugParam['user'] === true)?true:false;
		$bDisplaySess=(isset($aDebugParam['session']) and $aDebugParam['session'] === true)?true:false;
	}
}	
define("RGL_DBG_FLAG_DISPLAY",$flagDisplay);
define("RGL_DBG_DISPLAY_GET",$bDisplayGet);
define("RGL_DBG_DISPLAY_POST",$bDisplayPost);
define("RGL_DBG_DISPLAY_USER",$bDisplayUser);
define("RGL_DBG_DISPLAY_SESS",$bDisplaySess);

// verifica sequenza pagine 
//dbg_('objSess->pageSequence='.$objSess->pageSequence.', xsn='.$_REQUEST['xsn']);
if (!$isPopUp) {	
	if ($objSess->pageSequence != $pageSeq) {
		rglLogWarn('No sequence: objSess->pageSequence='.$objSess->pageSequence.', xsn='.$_REQUEST['xsn']);
		//define('XSN',"$subSess.".$objSess->pageSequence);require_once(RGL_FS_WEB_ROOT."/app/_inc/inc.set1.php");
		
		//dbg_('Redirect:'.$objSess->getHistory(0,true));
		//header("location: ".$objSess->getHistory(0,true));
		//exit();
	} 
	
	$objSess->pageSequence++;
	//$pageSeq = $objSess->pageSequence;
	if (!isset($noHistoryPost)) $noHistoryPost = false;
	if (isset($_REQUEST['hstrybck'])) { //history set
			$objSess->setHistory($_REQUEST['hstrybck']);
	} 
	if ($isReloadable) {
		$objSess->pushHistory($noHistoryPost);
	}
}
//$sDbg = "";
//foreach($objSess->history as $k => $aX) $sDbg  .= $k.")".$aX['page'].", ";
//dbg_($sDbg);
define('XSN',"$subSess.".$objSess->pageSequence);
$sXSNQS = "xsn=".urlencode(XSN);
$objSess->mcrTmStrt = microtime(true); //setta l'ora inizio pagina
// aggiorna tabella session
$sTimeNow = date('Y-m-d H:i:s');
$msgPag = (reqVar('msgbck')!='')?"<br>".reqVar('msgbck'):"";
//dbg_("newXSN=".XSN);
